﻿using AutoMapper;
using MediatR;
using SmartProduction.Domain.客户管理.WorkOrder;
using SmartProduction.ErrorCode;
using SmartProduction.Infarstructrue.Interface;
using SmartProduction.Write.API.Application.Commands.work_SalesOrderCommand;

namespace SmartProduction.Write.API.Application.CommandHandlers.work_SalesOrderHandler
{
    /// <summary>
    /// 销售订单删除命令处理
    /// </summary>
    public class work_SalesOrderDelCommandHandler : IRequestHandler<work_SalesOrderDelCommand, APIResult<object>>
    {
        /// <summary>
        /// 销售订单仓储
        /// </summary>
        private readonly IBaseRepository<work_SalesOrder> salesRepository;

        /// <summary>
        /// 映射
        /// </summary>
        private readonly IMapper mapper;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="salesRepository">销售订单仓储</param>
        /// <param name="mapper">映射</param>
        public work_SalesOrderDelCommandHandler(IBaseRepository<work_SalesOrder> salesRepository,IMapper mapper)
        {
            this.salesRepository = salesRepository;
            this.mapper = mapper;
        }


        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回</returns>
        public  async Task<APIResult<object>> Handle(work_SalesOrderDelCommand request, CancellationToken cancellationToken)
        {
            try
            {
                APIResult<object> result = new APIResult<object>();
                var obj = salesRepository.GetAll().Result.FirstOrDefault(x=>x.work_SalesOrderId == request.work_SalesOrderId);
                obj.IsDelete = true;
                var i = salesRepository.UpdateAsync(obj).Result;
                if(i > 0)
                {
                    result.Code = APIEnums.删除成功;
                    result.Msg = APIEnums.删除成功.ToString();
                    result.data = i;
                }
                else
                {
                    result.Code = APIEnums.删除失败;
                    result.Msg = APIEnums.删除失败.ToString();
                    result.data = i;
                }
                return await Task.FromResult(result);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
